# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User

import hashlib, time
from django.conf import settings

class Profile(models.Model):
    user = models.OneToOneField(User)
    bio = models.TextField(max_length=400, null=True, blank=True, 
        help_text="A short bio about yourself.  Maximum of 400 characters.")
    twitter = models.CharField(max_length=30, help_text="Your Twitter username (if you have one).", null=True, blank=True)
    reg_ip = models.IPAddressField(verbose_name="Registration IP")
    last_ip = models.IPAddressField(verbose_name="Last Known IP")
    reg_date = models.DateTimeField(auto_now_add=True, verbose_name="Registration Date")
    reg_key = models.CharField(max_length=32, verbose_name="Registration Key")
    options = models.OneToOneField('OptionList')
    
    class Meta:
        ordering = ('user__username', '-reg_date')
    
    def __unicode__(self):
        return u"%s's Profile" % self.user.username
    
    def generate_reg_key(self):
        """Generates the registration key for this user.
            The registration key is a hash of the current timestamp, the secret key, and the user's username"""
        return hashlib.md5("%s|%s|%s" % (time.time(), settings.SECRET_KEY, self.user.username)).hexdigest()

class OptionList(models.Model):
    #Make sure all options have defaults!
    pass